# Build a sample vectorDB
from langchain.retrievers import MultiQueryRetriever
from langchain_openai import ChatOpenAI
from langchain_chroma import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_ollama import OllamaEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
import logging

# 使用Ollama的嵌入模型
embeddings_model = OllamaEmbeddings(
        base_url='http://192.168.43.158:11434',
        model="nomic-embed-text")
# Load blog post
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
data = loader.load()

# Split
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
splits = text_splitter.split_documents(data)

# VectorDB
vectordb = Chroma.from_documents(documents=splits, embedding=embeddings_model)

# 配置 DeepSeek API（deepseek 兼容 OpenAI）
llm = ChatOpenAI(
    api_key = 'sk-xxxxxx',
    base_url = 'https://api.deepseek.com/v1',
    model='deepseek-chat'# 或其他 DeepSeek 模型
)
question = "What are the approaches to Task Decomposition?"
retriever_from_llm = MultiQueryRetriever.from_llm(
    retriever=vectordb.as_retriever(), llm=llm
)
logging.basicConfig()
logging.getLogger("langchain.retrievers.multi_query").setLevel(logging.INFO)

unique_docs = retriever_from_llm.invoke(question)
print(len(unique_docs))

# 添加分隔符和详细打印
print("\n" + "="*50)
print("UNIQUE DOCUMENTS DETAILS:")
print("="*50)

for i, doc in enumerate(unique_docs):
    print(f"\n--- Document {i+1} ---")
    print(f"Content: {doc.page_content[:100]}...")  # 显示前100个字符
    if hasattr(doc, 'metadata') and doc.metadata:
        print(f"Metadata: {doc.metadata}")
    print("-" * 30)
